/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-15
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-13
 * @Description: 
 */
import 'package:chat_client/config/api_config.dart';
import 'package:chat_client/models/apply_model.dart';
import 'package:chat_client/providers/application_provider.dart';
import 'package:chat_client/widgets/custom_global_widgets.dart';
import 'package:flutter/material.dart';
import 'package:chat_client/enums/apply_enum.dart';
import 'package:provider/provider.dart';

class FriendApplicationScreen extends StatelessWidget {
  const FriendApplicationScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('新的朋友')),
      body: _buildApplicationList(),
    );
  }

  Widget _buildApplicationList() {
    return Consumer<ApplicationProvider>(
      builder: (context, model, child) {
        if (model.applicationList.isEmpty) {
          return const _EmptyApplicationHint();
        }
        return ListView.separated(
          itemCount: model.applicationList.length,
          itemBuilder: (context, index) => _ApplicationListItem(
            apply: model.applicationList[index],
          ),
          separatorBuilder: (context, index) => const Divider(height: 1),
        );
      },
    );
  }
}

class _ApplicationListItem extends StatelessWidget {
  final Apply apply;

  const _ApplicationListItem({required this.apply});

  @override
  Widget build(BuildContext context) {
    final model = context.read<ApplicationProvider>();
    return CListTile(
      imageUrl: AppConfig.baseUrl + apply.opponentAvatarUrl,
      title: apply.opponentNikename,
      subtitle: apply.isSend ? '我：${apply.applyInfo}' : apply.applyInfo,
      onTap: () {
        if (apply.status != ApplyStatus.pending.value || apply.isSend == true) {
          return;
        }
        BottomMenu.showBottomMenu(context, [
          BottomMenuItem(
            title: "接受申请",
            icon: Icons.photo_library,
            onTap: () {
              model.handleFriendRequest(
                apply.opponentId,
                true,
                apply.applyInfo,
              );
              Navigator.pop(context);
            },
          ),
          BottomMenuItem(
            title: "拒绝申请",
            icon: Icons.download,
            onTap: () {
              model.handleFriendRequest(apply.opponentId, false, null);
              Navigator.pop(context);
            },
          )
        ]);
      },
      trailing: _ApplicationTrailing(
        isSend: apply.isSend,
        status: apply.status,
        opponentId: apply.opponentId,
      ),
    );
  }
}

class _ApplicationTrailing extends StatelessWidget {
  final bool isSend;
  final int status;
  final String opponentId;

  const _ApplicationTrailing({
    required this.isSend,
    required this.status,
    required this.opponentId,
  });

  @override
  Widget build(BuildContext context) {
    return isSend ? _buildSentStatus() : _buildReceivedStatus(context);
  }

  Widget _buildSentStatus() {
    return _StatusRow(
      icon: Icons.send,
      statusText: ApplyStatus.fromInt(status).label,
    );
  }

  Widget _buildReceivedStatus(BuildContext context) {
    return _StatusRow(
      icon: Icons.call_received,
      color: status == ApplyStatus.pending.value ? Color(0xFFABAFF5) : null,
      statusText: ApplyStatus.fromInt(status).label,
    );
  }
}

class _StatusRow extends StatelessWidget {
  final IconData icon;
  final String statusText;
  final Color? color;

  const _StatusRow({
    required this.icon,
    required this.statusText,
    this.color,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(icon, size: 24, color: color ?? Colors.grey[600]),
        const SizedBox(width: 14),
        Text(
          statusText,
          style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                color: color ?? Colors.grey[600],
              ),
        ),
      ],
    );
  }
}

class _EmptyApplicationHint extends StatelessWidget {
  const _EmptyApplicationHint();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(
        '暂无好友申请',
        style: Theme.of(context).textTheme.titleMedium?.copyWith(
              color: Colors.grey,
            ),
      ),
    );
  }
}
